﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.Composition;
using Core.Common;
using Core.BusinessLogic.Interfaces;
using Core.DataAccess;
using Core.Interfaces;
using SonCa.SocialNetwork.Accounts.IPresenters;

namespace SonCa.SocialNetwork.Accounts.Presenters
{
    public class RegisterPresenter
    {
        private IRegister _view;
        [Import]
        private IAccountService _accountService;
        [Import]
        private IWebContext _webContext;
        [Import]
        private IPermissionService _permissionService;
        [Import]
        private IEmail _email;
        [Import]
        private IConfiguration _configuration;
        [Import]
        private IRedirector _redirector;
        [Import]
        private IStatusChatService _statusChatService;

        public void Init(IRegister View)
        {
            _view = View;
            MEFManager.Compose(this);

            //_view.LoadTerms(_termRepository.GetCurrentTerm());
            //_view.LoadreCaptchaSetting((bool)_configuration.GetConfigurationSetting(typeof(bool), "reCaptcha"));
        }

        public void LoginLinkClicked()
        {
            //_redirector.GoToAccountLoginPage();
        }

        public void Register(string Username, string Password,string lastName, string middleName, 
                             string firstName, string Email, DateTime BirthDate, bool isCaptchaValid, 
                             bool AgreesWithTerms, Int32 TermID)
        {
            if (AgreesWithTerms)
            {
                if (isCaptchaValid)
                {
                    Core.DataAccess.Account acc = new Core.DataAccess.Account();
                    acc.LastName = lastName;
                    acc.MidName = middleName;
                    acc.FirstName = firstName;
                    acc.Email = Email;
                    acc.BirthDate = BirthDate;
                    acc.Username = Username;
                    acc.Plaintext = Core.Common.Cryptography.GeneratePlaintext();
                    acc.Password = Extensions.Encrypt(Password, acc.Plaintext);
                    acc.TermID = TermID;
                    //TODO: For development marked as verified already
                    acc.EmailVerified = false;
                    acc.CreateDate = DateTime.Now;                    
                    acc.LastUpdateDate = DateTime.Now;
                    acc.CountLogin = 0;
                    acc.IsLock = false;
                    acc.LevelID = 1;
                    acc.Score = 0;

                    if (_accountService.EmailInUse(Email))
                    {
                        _view.ShowErrorMessage("This email is already in use!");
                        //_view.ToggleWizardIndex(0);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                    }
                    else if (_accountService.UsernameInUse(Username))
                    {
                        _view.ShowErrorMessage("This username is already in use!");
                        //_view.ToggleWizardIndex(0);
                    }
                    else
                    {
                        _accountService.SaveAccount(acc);

                        _statusChatService.SaveStatus(new Status { AccountID = acc.AccountID,AccountStatus = false });

                        List<Permission> permissions = _permissionService.GetPermissionByName("PUBLIC");
                        Core.DataAccess.Account newAccount = _accountService.GetAccountByEmail(Email);

                        if (permissions.Count > 0 && newAccount != null)
                        {
                            _accountService.AddPermission(newAccount, permissions[0]);
                        }

                       _email.SendEmailAddressVerificationEmail(acc.Username, acc.Email);
                       _redirector.GoToRegisterSuccessPage();
                        //_view.ShowAccountCreatedPanel();
                    }
                }
                else
                {
                    _view.ShowErrorMessage("Your entry doesn't match the reCAPTCHA image. Please try again.");
                }
            }
            else
            {
                //_view.ToggleWizardIndex(2);
                _view.ShowErrorMessage("You can't create an account on this site if you don't agree with our terms!");
            }
        }
    }
}